package com.msf.modules.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.msf.common.domain.query.PageQuery;
import com.msf.common.domain.vo.Result;
import com.msf.modules.sys.domain.dto.RoleUserDto;
import com.msf.modules.sys.domain.dto.SysUserRoleDto;
import com.msf.modules.sys.domain.dto.UserDto;
import com.msf.modules.sys.domain.dto.UserEditDto;
import com.msf.modules.sys.entity.SysUser;
import com.msf.modules.sys.service.ISysUserRoleService;
import com.msf.modules.sys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xujf
 * @since 2024-07-24
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService userService;

    private final ISysUserRoleService sysUserRoleService;

    @ApiOperation("用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<SysUser>> queryPageList(PageQuery pageQuery) {
        return userService.queryPageList(pageQuery);
    }

    @ApiOperation("添加用户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<?> add(UserDto userDto){
        return userService.add(userDto);
    }

    @ApiOperation("修改用户")
    @PostMapping(value = "/edit")
    public Result<?> edit(UserEditDto userDto){
        return userService.edit(userDto);
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @ApiModelProperty(value = "用户id", required = true)
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        userService.deleteUser(id);
        return Result.ok("删除用户成功");
    }

    /**
     * 批量删除用户
     */
    @ApiOperation("批量删除用户")
    @ApiModelProperty(value = "用户id集合“，”分隔",required = true)
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        userService.deleteBatchUsers(ids);
        return Result.ok("批量删除用户成功");
    }

    @ApiOperation("用户详情")
    @ApiModelProperty(value = "用户id",required = true)
    @GetMapping(value = "/queryById")
    public Result<SysUser> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<SysUser> result = new Result<SysUser>();
        SysUser sysUser = userService.getById(id);
        if (sysUser == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(sysUser);
            result.setSuccess(true);
        }
        return result;
    }

    @ApiOperation("查询用户角色")
    @RequestMapping(value = "/queryUserRole", method = RequestMethod.GET)
    public Result<List<String>> queryUserRole(@RequestParam(name = "userId", required = true) String userId) {
        return userService.queryUserRole(userId);
    }

    @ApiOperation("查询角色用户")
    @RequestMapping(value = "/queryRoleUser", method = RequestMethod.GET)
    public Result<?> queryRoleUser(RoleUserDto roleUserDto) {
        return userService.queryRoleUser(roleUserDto);
    }

    @ApiOperation("添加角色用户")
    @RequestMapping(value = "/addSysUserRole", method = RequestMethod.POST)
    public Result<?> addSysUserRole(SysUserRoleDto sysUserRoleDto) {
        return userService.addSysUserRole(sysUserRoleDto);
    }

    @ApiOperation("删除角色用户")
    @RequestMapping(value = "/deleteRoleUser", method = RequestMethod.POST)
    public Result<?> deleteRoleUser(SysUserRoleDto sysUserRoleDto) {
        return userService.deleteRoleUser(sysUserRoleDto);
    }


    @ApiOperation("重置密码")
    @RequestMapping(value = "/restPassword", method = RequestMethod.GET)
    public Result<?> restPassword(@RequestParam(name = "userId", required = true) String userId) {
        return userService.restPassword(userId);
    }

}
